package com.ziyun.erp.modules.promotion.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ziyun.erp.modules.promotion.dao.UserCouponExchangeDao;
import com.ziyun.erp.modules.promotion.entity.UserCouponExchangeEntity;
import com.ziyun.erp.modules.promotion.service.UserCouponExchangeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("userCouponExchangeService")
public class UserCouponExchangeServiceImpl extends ServiceImpl<UserCouponExchangeDao, UserCouponExchangeEntity> implements UserCouponExchangeService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		Page<UserCouponExchangeEntity> page = this.selectPage(
				new Query<UserCouponExchangeEntity>(params).getPage(),
				new EntityWrapper<UserCouponExchangeEntity>().like(StringUtils.isNotBlank((String) params.get("exchangeCode")), "exchange_code", (String) params.get("exchangeCode"))
						.like(StringUtils.isNotBlank((String)params.get("userId")), "user_id", (String) params.get("userId"))
		                .eq(StringUtils.isNotBlank((String) params.get("used")), "used", (String) params.get("used"))
						.gt(StringUtils.isNotBlank((String) params.get("beginTime")), "create_date", (String) params.get("beginTime"))
						.lt(StringUtils.isNotBlank((String) params.get("endTime")), "create_date", (String) params.get("endTime"))
						.orderBy("create_date",false));
		return new PageUtils(page);
	}

	@Override
	public R createCode(Map<String, Object> params) {
		String couponIds = params.get("couponIds").toString();
		Integer count = Integer.valueOf(params.get("count").toString());
		List<UserCouponExchangeEntity> entityList = new ArrayList<>();

		Set<Integer> set = getCouponCode(count);
		for (Integer code : set) {
			UserCouponExchangeEntity userCouponExchangeEntity = new UserCouponExchangeEntity();
			userCouponExchangeEntity.setCouponIds(couponIds);
			userCouponExchangeEntity.setExchangeCode(Long.valueOf(code));
			entityList.add(userCouponExchangeEntity);
		}

		this.insertBatch(entityList);
		return R.ok();
	}

	private Set<Integer> getCouponCode(Integer count) {
		Set<Integer> set = new HashSet<>();
		while (true) {
			String code = "";
			//随机生成1位数,判断code是8位还是9位
			Integer random = Integer.valueOf(getRandom(1));
			if (random % 2 == 0) {//8
				code = getRandom(3) + getRandom(3) + getRandom(2);
			} else {
				code = getRandom(3) + getRandom(3) + getRandom(2) + getRandom(1);
			}
			Integer v = Integer.valueOf(code);
			if (v > 10000000) {
				set.add(Integer.valueOf(code));
			}
			if (set.size() >= count) {
				break;
			}
		}
		//查数据库是否有重复的
		List<UserCouponExchangeEntity> list = this.baseMapper.selectList(new EntityWrapper<UserCouponExchangeEntity>().in("exchange_code", StringUtils.join(set.toArray(), ",")));
		if (list.size() > 0) {
			for (UserCouponExchangeEntity userCouponExchange : list) {
				set.remove(userCouponExchange.getExchangeCode());
			}
			Set<Integer> setNew = getCouponCode(list.size());
			set.addAll(setNew);
			return set;
		}

		return set;
	}

	private String getRandom(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			val += String.valueOf(random.nextInt(10));
		}
		return val;
	}


}
